package com.sztx.worker_monitor.web.handle;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.ui.Model;

import com.sztx.worker_monitor.common.Md5Util;
import com.sztx.worker_monitor.dataaccess.dao.mysql.UserInfoMysqlDAO;
import com.sztx.worker_monitor.dataaccess.domain.UserInfoDO;
import com.sztx.worker_monitor.web.model.PwdInfoMaker;
import com.sztx.worker_monitor.web.model.PwdInfoModel;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

@Component
public class LoginHandle {
	
	@Autowired(required = false)
	private UserInfoMysqlDAO userInfoMysqlDAO;
	
	public LoginHandle(){}

	static private HttpServletRequest getHttpServletRequest() {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
		return request;
	}
	
	//-1：未登录  ,其余返回用户type  1：管理员 2：超级管理员
	static public Integer checkLogin(){
		HttpServletRequest request = getHttpServletRequest();
		HttpSession session = request.getSession();
		System.out.println(">>>>>>>checkLogin->seID="+session.getId());
		Object obloginFlag = session.getAttribute("loginFlag");
		if(obloginFlag == null) return -1;
		try{
			Integer loginFlag = (Integer) obloginFlag;
			return  loginFlag;
		}catch(Exception e){
			return -1;
		}
	}
	
	 public String getSalt(){
		
		HttpServletRequest request = getHttpServletRequest();
		HttpSession session = request.getSession();
		System.out.println("getUserName->session="+session+",seID="+session.getId());
		String salt = (String) session.getAttribute("salt");
		return salt;
	}
	
	 public String getDisturbance(){
		HttpServletRequest request = getHttpServletRequest();
		HttpSession session = request.getSession();
		System.out.println("getUserName->session="+session+",seID="+session.getId());
		String salt = (String) session.getAttribute("disturbance");
		return salt;
	}
	
	 public String getUserName(){
		 HttpServletRequest request = getHttpServletRequest();
		HttpSession session = request.getSession();
		System.out.println("getUserName->session="+session+",seID="+session.getId());
		String userName = (String) session.getAttribute("userName");

		return userName;
	}
	
	 public void setLoginInfo(Model model){
		System.out.println("-->setLoginInfo setLoginInfo");
		model.addAttribute("loginFlag", checkLogin());
		model.addAttribute("userName", getUserName());
	}
	
	
	/**
	 * 登录的用户名验证，返回值为错误，描述，其中做一些保存处理，
	 * **/
	 public String checkName(PwdInfoModel pwdInfoModel){
		HttpServletRequest request = getHttpServletRequest();
		HttpSession session = request.getSession();
		if(pwdInfoModel == null) return "内部错误";
		String userName 	= request.getParameter("userName");
		if(userName == null || "".equals(userName) || userName.length() <2 ){
			session.removeAttribute("disturbance");
			return "账号不存在";
		}
			
		//判断该用户是否已经存在
		try{
			UserInfoDO serInfoDO = userInfoMysqlDAO.findByUsername(userName);
			if(null == serInfoDO) {
				session.removeAttribute("disturbance");
				return "账号不存在";
			}
			
			//删除登录信息
			session.removeAttribute("userName");
			session.removeAttribute("loginFlag");
			
			
			String disturbance = PwdInfoMaker.makeDisturbance();
			
			pwdInfoModel.setSalt(serInfoDO.getSalt());
			pwdInfoModel.setDisturbance(disturbance);
			
			session.setAttribute("disturbance", disturbance);

			return null;	
		}catch(Exception e){
			e.printStackTrace();
			session.removeAttribute("disturbance");
			return "数据库查询错误" +e.toString();
		}
	}
	
	/**
	 * 登录用户
	 * 
	 * */
	public String userLogin(){
		HttpServletRequest request = getHttpServletRequest();
		if(request == null) return "内部错误";
		String userName = request.getParameter("userName");
		String checkWord= request.getParameter("checkWord");
		
		if(userName == null || "".equals(userName) || userName.length() <2 )
			return "账号不存在";

		UserInfoDO userInfoDO =null;
		//判断该用户是存在
		try{
			userInfoDO = userInfoMysqlDAO.findByUsername(userName);
			if(null == userInfoDO) return "账号不存在";
		}catch(Exception e){
			e.printStackTrace();
			return "数据库查询错误" +e.toString();
		}
		String disturbance = getDisturbance();
		if(disturbance == null || "".equals(disturbance)) return "登录错误" ;
		String md5all = Md5Util.getMd5(userInfoDO.getMd5PwdSalt() + disturbance);
		if(md5all.equals(checkWord)) {
			//登陆成功
			HttpSession session = request.getSession();
			System.out.println("userLogin->session="+session+",seID="+session.getId());
			session.setAttribute("userName", userName);
			session.setAttribute("loginFlag", userInfoDO.getType());
	
			return null;
		}
		else
			return "密码不正确";
	
	}
	
	static  public String logout() {
		 
		HttpServletRequest request = getHttpServletRequest();
		if(request == null) return "内部错误";
		
		HttpSession session = request.getSession();
		
		session.removeAttribute("userName");
		session.removeAttribute("loginFlag");

		return null;
		
	}
	
	
	
	
	
	
	
}
